<?php

/**
 * This is the model class for table "tbl_image".
 *
 * The followings are the available columns in table 'tbl_image':
 * @property string $id
 * @property string $title
 * @property string $img_url
 * @property integer $status
 * @property integer $like_cnt
 * @property integer $view_cnt
 * @property string $user_created
 * @property string $created_at
 *
 * The followings are the available model relations:
 * @property Collection[] $collection
 * @property Comment[] $comment
 * @property User $userCreated
 * @property ImageTag[] $imageTag
 * @property User[] $userFollow
 * @property UserActive[] $userActive
 * @property UserNotify[] $userNotify
 */
class Image extends CActiveRecord {

    const IMAGE_LOCKED = 0;
    const IMAGE_ACTIVE = 1;
    const IMAGE_PENDING = 2;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Image the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_image';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title, img_url, user_created, created_at', 'required', 'on' => 'upload'),
            array('status, like_cnt, view_cnt', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 200),
            array('img_url', 'length', 'max' => 100),
            array('img_url', 'file', 'types' => 'jpg, gif, png', 'maxSize' => 1024 * 1024 * 2, 'tooLarge' => 'File không được quá 2Mb !', 'allowEmpty' => true, 'on' => 'ajaxupdate,edit'),
            array('user_created', 'length', 'max' => 20),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, title, img_url, status, like_cnt, view_cnt, user_created, created_at', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'collection' => array(self::MANY_MANY, 'Collection', 'tbl_collection_image(image_id, collection_id)'),
            'comment' => array(self::HAS_MANY, 'Comment', 'image_id'),
            'userCreated' => array(self::BELONGS_TO, 'User', 'user_created'),
            'imageTag' => array(self::HAS_MANY, 'ImageTag', 'image_id'),
            'userFollow' => array(self::MANY_MANY, 'User', 'tbl_image_user_follow(image_id, user_id)'),
            'userActive' => array(self::HAS_MANY, 'UserActive', 'image_id'),
            'userNotify' => array(self::HAS_MANY, 'UserNotify', 'image_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'title' => 'Tiêu đề',
            'img_url' => 'Ảnh',
            'status' => 'Status',
            'like_cnt' => 'Lượt like',
            'view_cnt' => 'Lượt xem',
            'user_created' => 'Người Upload',
            'created_at' => 'Upload lúc',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('img_url', $this->img_url, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('like_cnt', $this->like_cnt);
        $criteria->compare('view_cnt', $this->view_cnt);
        $criteria->compare('user_created', $this->user_created, true);
        $criteria->compare('created_at', $this->created_at, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}